/*
 * @Description:管理员api
 * @Author: maskMan
 * @LastEditTime: 2025-03-05 11:19:49
 */
import { resSend } from "../public/httpServe";
import Mondb from "../mongo/mondb";
import { Table } from "../mongo/struct";
import { util } from "../public/util";
import Joint from "../mongo/joint";
import { rewordWeight } from "../public/gameApp";
const express = require("express");
const router = express.Router();
//获取角色权限
router.get("/getManageRoles", async (req, res) => {
  try {
    const { id, account, ident } = req.auth;
    if (id && account && ident) {
      const adminUser: any = await Mondb.findOneById(Table.admin, id, ["roles", "name", "ipone", "parent"]);
      if (adminUser) {
        const rawData = {
          manager: {
            ident: "admin",
            account: account,
            name: adminUser.name,
            parent: adminUser.parent || "",
            phone: adminUser.ipone || "",
            lastTime: util.formaDate("年-月-日 时:分:秒"),
            avaurl: "https://main.lzmgame.com/headers/head.png"
          },
          roles: adminUser.roles
        };
        return resSend(res, rawData, true, "登录成功");
      }
    }
    return resSend(res, false, null, "登录失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//获取用户数
router.get("/getCounts", async (req, res) => {
  try {
    const { id, account, ident } = req.auth;
    if (id && account && ident) {
      const dayTime = -24 * 60 * 60 * 1000;
      const today = new Date(util.formaDate("年-月-日")).getTime() - dayTime;
      const weekDay = new Date(util.formaDate("年-月-日")).getTime() - 7 * dayTime;
      const monDay = new Date(util.formaDate("年-月-日")).getTime() - 30 * dayTime;
      if (ident == "customer") {
        const userList = await Mondb.findManyData(Table.user, { parent: account });
        let totalCount = userList.length;
        let monthCount = 0;
        let weekCount = 0;
        let dayCount = 0;
        let activeCount = 0;
        for (let index = 0; index < userList.length; index++) {
          const regTime = new Date(userList[index]["regTime"]).getTime();
          if (regTime >= monDay) {
            monthCount++;
          }
          if (regTime >= weekDay) {
            weekCount++;
          }
          if (regTime >= today) {
            dayCount++;
          }
          const activeTime = new Date(userList[index]["activeTime"]).getTime();
          if (activeTime >= weekDay) {
            activeCount++;
          }
        }
        const data = {
          直属用户: {
            totalCount,
            monthCount,
            weekCount,
            dayCount,
            activeCount
          }
        };
        return resSend(res, data, true, "获取成功");
      }
      if (ident == "admin") {
        const userList = await Mondb.findManyData(Table.user, { parent: account });
        let totalCount = userList.length;
        let monthCount = 0;
        let weekCount = 0;
        let dayCount = 0;
        let activeCount = 0;

        for (let index = 0; index < userList.length; index++) {
          const regTime = new Date(userList[index]["regTime"]).getTime();
          if (regTime >= monDay) {
            monthCount++;
          }
          if (regTime >= weekDay) {
            weekCount++;
          }
          if (regTime >= today) {
            dayCount++;
          }
          const activeTime = new Date(userList[index]["activeTime"]).getTime();
          if (activeTime >= weekDay) {
            activeCount++;
          }
        }
        const otherList = await Mondb.findManyData(Table.user, { parent: { $ne: account } });
        let ntotalCount = otherList.length;
        let nmonthCount = 0;
        let nweekCount = 0;
        let ndayCount = 0;
        let nactiveCount = 0;
        for (let index = 0; index < otherList.length; index++) {
          const regTime = new Date(otherList[index]["regTime"]).getTime();
          if (regTime >= monDay) {
            nmonthCount++;
          }
          if (regTime >= weekDay) {
            nweekCount++;
          }
          if (regTime >= today) {
            ndayCount++;
          }
          const activeTime = new Date(otherList[index]["activeTime"]).getTime();
          if (activeTime >= weekDay) {
            nactiveCount++;
          }
        }
        const customerCount = await Mondb.countDocument(Table.admin, { ident: "customer" });
        let data = {
          直属用户: {
            totalCount,
            monthCount,
            weekCount,
            dayCount,
            activeCount
          },
          客服用户: {
            totalCount: ntotalCount,
            monthCount: nmonthCount,
            weekCount: nweekCount,
            dayCount: ndayCount,
            activeCount: nactiveCount
          },
          客服管理: {
            totalCount: customerCount
          }
        };
        return resSend(res, data, true, "获取成功");
      }
    }
    return resSend(res, false, null, "登录失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//更新角色信息
router.post("/updateManageData", async (req, res) => {
  try {
    const { id, account, ident } = req.auth;
    const { newpassword, ipone, name, oldpassword } = req.body;
    if (id && account && ident) {
      const sd = { password: newpassword };
      ipone && (sd["ipone"] = ipone);
      name && (sd["name"] = name);
      const result = await Mondb.updateOneData(Table.admin, { account, password: oldpassword }, sd);
      if (result) {
        return resSend(res, result ? true : false, true, "修改成功");
      }
      return resSend(res, false, null, "修改失败");
    }
    return resSend(res, false, null, "修改失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//获取客服列表
router.get("/getAgentList", async (req, res) => {
  try {
    const { id, account, ident } = req.auth;
    const { page = 1, limit = 500000, sort } = req.query;
    if (id && account && ident) {
      const findAccount = req.query.account;
      const keyList = ["account", "ipone", "name", "parent", "regTime", "lastTime", "status", "loginCount", "customerCount"];
      const list = await Mondb.findManyData(Table.admin, { parent: account, ident: "customer" }, keyList);
      const count = await Mondb.countDocument(Table.user, { parent: account });
      if (list) {
        const result = [];
        for (let i = 0; i < list.length; i++) {
          const item = list[i];
          const obj = {
            id: i + 1,
            account: item["account"],
            phone: item["ipone"] || "",
            name: item["name"] || "",
            parent: item["parent"] || "",
            regTime: item["regTime"],
            lastTime: item["lastTime"],
            status: item["status"],
            directCount: count
          };
          result.push(obj);
        }
        let mockList = result.filter(v => {
          if (findAccount) {
            const regex = new RegExp(".*" + findAccount + ".*", "i");
            if (regex.test(v.account)) {
              return true;
            }
          } else {
            return true;
          }
        });
        if (sort === "-id") {
          mockList.sort((x, y) => {
            return Number(x.regTime) - Number(y.regTime);
          });
        } else {
          mockList.sort((x, y) => {
            return Number(y.regTime) - Number(x.regTime);
          });
        }
        let pageList;
        if (page == -1) {
          pageList = mockList;
        } else {
          pageList = mockList.filter(
            (_: any, index: number) => index < (limit as number) * (page as number) && index >= (limit as number) * ((page as number) - 1)
          );
        }
        return resSend(res, { total: mockList.length, items: pageList }, true, "获取成功");
      }
    }
    return resSend(res, false, null, "失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//创建代理账号
router.post("/createAgent", async (req, res) => {
  try {
    const { id, ident } = req.auth;
    const { account, phone, name, password, status } = req.body;
    if (id && account && ident == "admin" && password) {
      const sd = { status, account, password, parent: "admin", ident: "customer", regTime: util.formaDate("年-月-日 时:分:秒") };
      phone && (sd["ipone"] = phone);
      name ? (sd["name"] = name) : (sd["name"] = account);
      const roles: any = {
        客服管理: { 创建权限: true, 编辑权限: true },
        客服用户: { 查看权限: true, 删除权限: true, 编辑权限: { 普通会员: true, 至尊会员: true, 高级会员: true } },
        直属用户: {
          查看权限: true,
          删除权限: true,
          编辑权限: { 普通会员: true, 至尊会员: true, 高级会员: true },
          创建权限: { 普通会员: true, 至尊会员: true, 高级会员: true }
        },
        游戏分类: { 创建权限: true, 删除权限: true, 编辑权限: true, 查看权限: true },
        奖励设置: { 编辑权限: true, 查看权限: true },
        礼物商城: { 编辑权限: true, 删除权限: true, 查看权限: true, 创建权限: true },
        操作记录: { 删除权限: true, 查看权限: true }
      };
      sd["roles"] = roles;
      const result: any = await Mondb.creatOneData(Table.admin, sd);
      if (result) {
        const agent = {
          id: 0,
          account: result.account,
          phone: result.ipone || "",
          name: result.name,
          parent: result.parent || "",
          regTime: result.regTime,
          lastTime: util.formaDate("年-月-日 时:分:秒"),
          status: result.status,
          directCount: 0
        };
        return resSend(res, agent, true, "创建成功");
      }
      return resSend(res, false, null, "创建失败");
    }
    return resSend(res, false, null, "创建失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//更新客服信息
router.post("/updateAgent", async (req, res) => {
  try {
    const { id, ident } = req.auth;
    const { account, ipone, name, password, status } = req.body;
    if (id && account && ident == "admin") {
      const agent = {
        password,
        status,
        name,
        ipone
      };
      for (const key in agent) {
        if (agent[key] == undefined || agent[key] == null) {
          delete agent[key];
        }
      }
      const result = await Mondb.findOneAndUpdate(Table.admin, { account }, agent);
      if (result) {
        delete result["roles"];
        delete result["_id"];
        return resSend(res, result, true, "修改成功");
      }
      return resSend(res, false, null, "修改失败");
    }
    return resSend(res, false, null, "修改失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//删除代理账号
router.post("/deleteAgent", async (req, res) => {
  try {
    const { id, ident } = req.auth;
    const account = req.body.account;
    if (id && account && ident) {
      const result: boolean = await Mondb.deleteOneData(Table.admin, { account });
      if (result) {
        return resSend(res, true, true, "删除成功");
      }
      return resSend(res, false, null, "删除失败");
    }
    return resSend(res, false, null, "删除失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//获取用户列表
router.get("/getUserList", async (req, res) => {
  try {
    const { id, account, ident } = req.auth;
    const { page = 1, limit = 500000, sort, junior } = req.query;
    if (id && account && ident) {
      const findAccount = req.query.account;
      let userList: any;
      if (ident == "admin" && junior) {
        userList = await Mondb.findManyData(Table.user, { parent: { $ne: account } });
      } else {
        userList = await Mondb.findManyData(Table.user, { parent: account });
      }
      if (userList) {
        const result = [];
        for (let i = 0; i < userList.length; i++) {
          const item = userList[i];
          const obj = {
            parent: item["parent"],
            account: item["account"],
            coins: item["coins"],
            points: item["points"],
            vipLevel: item["vipLevel"],
            name: item["name"] || "",
            age: item["age"] || 3,
            regTime: item["regTime"],
            endTime: item["endTime"],
            lastTime: item["lastTime"],
            status: item["status"],
            commit: item["commit"] || "",
            referrer: item["referrer"] || ""
          };
          result.push(obj);
        }
        let mockList = result.filter(v => {
          if (findAccount) {
            const regex = new RegExp(".*" + findAccount + ".*", "i");
            if (regex.test(v.account)) {
              return true;
            }
          } else {
            return true;
          }
        });
        if (sort === "-id") {
          mockList.sort((x, y) => {
            return Number(x.regTime) - Number(y.regTime);
          });
        } else {
          mockList.sort((x, y) => {
            return Number(y.regTime) - Number(x.regTime);
          });
        }
        let pageList;
        if (page == -1) {
          pageList = mockList;
        } else {
          pageList = mockList.filter(
            (_: any, index: number) => index < (limit as number) * (page as number) && index >= (limit as number) * ((page as number) - 1)
          );
        }
        for (let index = 0; index < pageList.length; index++) {
          pageList[index].id = index + 1;
        }
        return resSend(res, { total: mockList.length, items: pageList }, true, "获取成功");
      }
    }
    return resSend(res, false, null, "失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//创建用户账号
router.post("/createUser", async (req, res) => {
  try {
    const { id, ident } = req.auth;
    const creator = req.auth.account;
    if (!id || !ident) {
      return resSend(res, null, false, "创建权限不正确");
    }
    let { account, password, parent, coins, points, vipLevel, timeType, endTime, status, commit } = req.body;
    if (!account || !password || !parent) {
      console.log("createUser注册参数不正确", account, password, parent);
      return resSend(res, null, false, "createUser参数不正确");
    }
    let _endTime = null;
    if (timeType == "自定义" && endTime) {
      _endTime = endTime;
    } else if (timeType) {
      const day = 24 * 60 * 60 * 1000;
      const list = ["一星期", "一个月", "三个月", "六个月", "一整年"];
      const valueList = [day * 7, day * 30, day * 90, day * 180, day * 365];
      const fIndex = list.findIndex(v => v == timeType);
      if (fIndex >= 0) {
        _endTime = util.formaDate("年-月-日 时:分:秒", new Date().getTime() + valueList[fIndex]);
      }
    }
    const reg = {
      account: account,
      password: password,
      parent: parent || "admin",
      coins: coins || 0,
      points: points || 0,
      name: account,
      age: 3,
      endTime: _endTime,
      vipLevel: vipLevel || null,
      commit: commit || "",
      status: status
    };
    const result: any = await Joint.createUser(reg);
    if (!result) {
      console.log("createUser注册失败", reg);
      return resSend(res, null, false, "账号注册失败");
    }
    Joint.createActionRecord({
      account,
      creator,
      actionType: "用户创建",
      commit: reg as any
    });
    console.log("createUser注册成功", reg);
    const temp = {
      account: result.account,
      parent: result.parent || "admin",
      name: result.name,
      age: result.age,
      vipLevel: result.vipLevel || "免费会员",
      coins: result.coins || 0,
      points: result.points || 0,
      regTime: result.regTime,
      referrer: result.referrer || null,
      endTime: result.endTime || null,
      lastTime: null,
      commit: result.commit,
      status: result.status || false
    };
    return resSend(res, temp, true, "账号注册成功");
  } catch (error) {
    console.error("createUser系统错误", req, error);
    return resSend(res, null, false, "系统错误");
  }
});
//更新用户信息
router.post("/updateUser", async (req, res) => {
  try {
    const { id, ident } = req.auth;
    const creator = req.auth.account;
    const { account, password, parent, coins, points, vipLevel, timeType, status, endTime, commit } = req.body;
    if (id && account && ident) {
      let _endTime = null;
      if (timeType == "自定义" && endTime) {
        _endTime = endTime;
      } else if (timeType) {
        const day = 24 * 60 * 60 * 1000;
        const list = ["一星期", "一个月", "三个月", "六个月", "一整年"];
        const valueList = [day * 7, day * 30, day * 90, day * 180, day * 365];
        const fIndex = list.findIndex(v => v == timeType);
        if (fIndex >= 0) {
          _endTime = util.formaDate("年-月-日 时:分:秒", new Date().getTime() + valueList[fIndex]);
        }
      }
      const user = {
        password,
        parent,
        coins,
        points,
        vipLevel,
        timeType,
        status,
        endTime,
        commit
      };
      for (const key in user) {
        if (user[key] == undefined || user[key] == null) {
          delete user[key];
        }
      }
      const result: any = await Mondb.findOneAndUpdate(Table.user, { account }, user);
      if (result) {
        const temp = {
          account: result.account,
          parent: result.parent || "admin",
          name: result.name,
          age: result.age,
          vipLevel: result.vipLevel || "免费会员",
          coins: result.coins || 0,
          points: result.points || 0,
          regTime: result.regTime,
          referrer: result.referrer || null,
          endTime: result.endTime || null,
          lastTime: null,
          commit: result.commit,
          status: result.status || false
        };
        Joint.createActionRecord({
          account,
          creator,
          actionType: "用户更新",
          commit: user as any
        });
        return resSend(res, temp, true, "修改成功");
      }
      return resSend(res, false, null, "修改失败");
    }
    return resSend(res, false, null, "修改失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//删除用户
router.post("/deleteUser", async (req, res) => {
  try {
    const { id, ident } = req.auth;
    const { account } = req.body;
    const creator = req.auth.account;
    if (id && account && ident) {
      const result: boolean = await Mondb.deleteOneData(Table.user, { account });
      if (result) {
        Joint.createActionRecord({
          account,
          creator,
          actionType: "用户更新",
          commit: account
        });
        return resSend(res, true, true, "删除成功");
      }
      return resSend(res, false, null, "删除失败");
    }
    return resSend(res, false, null, "删除失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//获取权重列表
router.get("/getWeightList", async (req, res) => {
  try {
    const { id, account, ident } = req.auth;
    if (id && account && ident) {
      const result = await Mondb.findOneData(Table.weight, { WID: 1 });
      if (result) {
        return resSend(res, result, true, "获取成功");
      }
    }
    return resSend(res, false, null, "失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//更新权重数据
router.post("/updateWeight", async (req, res) => {
  try {
    const { id, ident } = req.auth;
    const { timeBase1, timeBase2, scoreBase3, scoreRatio, coinsRatio, pointRatio } = req.body;
    if (id && ident) {
      const weight = {
        timeBase1,
        timeBase2,
        scoreBase3,
        scoreRatio,
        coinsRatio,
        pointRatio
      };
      for (const key in weight) {
        if (weight[key] == undefined || weight[key] == null) {
          delete weight[key];
        }
      }
      const result: any = await Mondb.findOneAndUpdate(Table.weight, { WID: 1 }, weight);
      if (result) {
        for (const key in result) {
          if (result[key] != undefined || result[key] != null) {
            rewordWeight[key] = result[key];
          }
        }
        console.log(rewordWeight);
        return resSend(res, result, true, "修改成功");
      }
      return resSend(res, false, null, "修改失败");
    }
    return resSend(res, false, null, "修改失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//获取商品列表
router.get("/getShopList", async (req, res) => {
  try {
    const { id, account, ident } = req.auth;
    if (id && account && ident) {
      const result = await Mondb.findManyData(Table.shop, {});
      if (result) {
        const resultList = [];
        for (let i = 0; i < result.length; i++) {
          result[i]["id"] = i + 1;
          result[i]["sid"] = result[i]["SID"];
          delete result[i]["SID"];
          resultList.push(result[i]);
        }
        return resSend(res, resultList, true, "获取成功");
      }
    }
    return resSend(res, false, null, "失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//更新商品信息
router.post("/updateShop", async (req, res) => {
  try {
    const { id, ident } = req.auth;
    const { sid, groupType, name, price, points, stock, detail, status, desc } = req.body;
    if (id && ident && sid) {
      const shop = {
        groupType,
        name,
        price,
        points,
        stock,
        desc,
        detail,
        status
      };
      for (const key in shop) {
        if (shop[key] == undefined || shop[key] == null) {
          delete shop[key];
        }
      }
      const result = await Mondb.findOneAndUpdate(Table.shop, { SID: sid }, shop);
      if (result) {
        return resSend(res, result, true, "修改成功");
      }
      return resSend(res, false, null, "修改失败");
    }
    return resSend(res, false, null, "修改失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//创建商品
router.post("/createShop", async (req, res) => {
  try {
    const { id, ident } = req.auth;
    const { groupType, name, price, points, stock, detail, status, desc } = req.body;
    if (id && ident) {
      const shop = {
        groupType,
        name,
        price,
        points,
        stock,
        desc,
        detail,
        status
      };
      const result: any = await Mondb.creatOneData(Table.shop, shop);
      if (result) {
        shop["sid"] = result["SID"];
        return resSend(res, shop, true, "创建成功");
      }
      return resSend(res, false, null, "创建失败");
    }
    return resSend(res, false, null, "创建失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//删除商品
router.post("/deleteShop", async (req, res) => {
  try {
    const { id, ident } = req.auth;
    const SID = req.body.id;
    if (id && SID && ident) {
      const result: boolean = await Mondb.deleteOneData(Table.shop, { SID });
      if (result) {
        return resSend(res, true, true, "删除成功");
      }
      return resSend(res, false, null, "删除失败");
    }
    return resSend(res, false, null, "删除失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//获取操作记录列表
router.get("/getActionList", async (req, res) => {
  try {
    const { id, account, ident } = req.auth;
    if (id && account && ident) {
      const findAccount = req.query.account;
      const day = 24 * 60 * 60 * 1000;
      let timeStamp;
      const { page = 1, limit = 500000, sort, junior, timeVal } = req.query;
      switch (timeVal) {
        case "7天":
          timeStamp = util.now() - 7 * day;
          break;
        case "15天":
          timeStamp = util.now() - 15 * day;
          break;
        case "30天":
          timeStamp = util.now() - 30 * day;
          break;
        case "180天":
          timeStamp = util.now() - 180 * day;
          break;
        case "365天":
          timeStamp = util.now() - 365 * day;
          break;
        default:
          timeStamp = util.now() - 30 * day;
          break;
      }
      let resultList, mockList, pageList;
      if (ident == "admin" && junior == "false") {
        resultList = await Mondb.findManyData(Table.actionRecord, { creator: account, timeStamp: { $gte: timeStamp } });
      } else {
        resultList = await Mondb.findManyData(Table.actionRecord, { creator: { $ne: account }, timeStamp: { $gte: timeStamp } });
      }
      if (resultList) {
        mockList = resultList.filter(v => {
          if (findAccount) {
            const regex = new RegExp(".*" + findAccount + ".*", "i");
            if (regex.test(v.account)) {
              return true;
            }
          } else {
            return true;
          }
        });
        if (sort === "-id") {
          mockList.sort((x, y) => {
            return Number(x.timeStamp) - Number(y.timeStamp);
          });
        } else {
          mockList.sort((x, y) => {
            return Number(y.timeStamp) - Number(x.timeStamp);
          });
        }
        if (page == -1) {
          pageList = mockList;
        } else {
          pageList = mockList.filter(
            (_: any, index: number) => index < (limit as number) * (page as number) && index >= (limit as number) * ((page as number) - 1)
          );
        }
        const recordList = [];
        for (let i = 0; i < pageList.length; i++) {
          pageList[i]["id"] = i + 1;
          recordList.push(pageList[i]);
        }
        return resSend(res, { total: mockList.length, items: recordList }, true, "获取成功");
      }
    }
    return resSend(res, false, null, "失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//删除操作记录
router.post("/deleteActionRecord", async (req, res) => {
  try {
    const { id, ident } = req.auth;
    const PID = req.body.id;
    if (id && PID && ident) {
      const result: boolean = await Mondb.deleteOneData(Table.actionRecord, { PID });
      if (result) {
        return resSend(res, true, true, "删除成功");
      }
      return resSend(res, false, null, "删除失败");
    }
    return resSend(res, false, null, "删除失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//获取商店记录列表
router.get("/getPayRecordList", async (req, res) => {
  try {
    const { id, account, ident } = req.auth;
    if (id && account && ident) {
      const findAccount = req.query.account;
      const day = 24 * 60 * 60 * 1000;
      let timeStamp;
      const { page = 1, limit = 500000, sort, timeVal } = req.query;
      switch (timeVal) {
        case "7天":
          timeStamp = util.now() - 7 * day;
          break;
        case "15天":
          timeStamp = util.now() - 15 * day;
          break;
        case "30天":
          timeStamp = util.now() - 30 * day;
          break;
        case "180天":
          timeStamp = util.now() - 180 * day;
          break;
        case "365天":
          timeStamp = util.now() - 365 * day;
          break;
        default:
          timeStamp = util.now() - 30 * day;
          break;
      }
      let resultList, mockList, pageList;
      resultList = await Mondb.findManyData(Table.shopRecord, { timeStamp: { $gte: timeStamp } });
      if (resultList) {
        mockList = resultList.filter(v => {
          if (findAccount) {
            const regex = new RegExp(".*" + findAccount + ".*", "i");
            if (regex.test(v.account)) {
              return true;
            }
          } else {
            return true;
          }
        });
        if (sort === "-id") {
          mockList.sort((x, y) => {
            return Number(x.timeStamp) - Number(y.timeStamp);
          });
        } else {
          mockList.sort((x, y) => {
            return Number(y.timeStamp) - Number(x.timeStamp);
          });
        }
        if (page == -1) {
          pageList = mockList;
        } else {
          pageList = mockList.filter(
            (_: any, index: number) => index < (limit as number) * (page as number) && index >= (limit as number) * ((page as number) - 1)
          );
        }
        const recordList = [];
        for (let i = 0; i < pageList.length; i++) {
          pageList[i]["id"] = i + 1;
          recordList.push(pageList[i]);
        }
        return resSend(res, { total: mockList.length, items: recordList }, true, "获取成功");
      }
    }
    return resSend(res, false, null, "失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//更新商店记录
router.post("/updatePayRecord", async (req, res) => {
  try {
    const { id, ident } = req.auth;
    const { ipone, name, address, status, orderIndex, commit, SRID } = req.body;
    if (id && SRID && ident) {
      const shop = {
        ipone,
        name,
        address,
        status,
        orderIndex,
        commit
      };
      for (const key in shop) {
        if (shop[key] == undefined || shop[key] == null) {
          delete shop[key];
        }
      }
      const result: any = await Mondb.findOneAndUpdate(Table.shopRecord, { SRID }, shop);
      if (result) {
        delete result["_id"];
        return resSend(res, result, true, "修改成功");
      }
      return resSend(res, false, null, "修改失败");
    }
    return resSend(res, false, null, "修改失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
//删除商店记录
router.post("/deletePayRecord", async (req, res) => {
  try {
    const { id, ident } = req.auth;
    const SRID = req.body.id;
    if (id && SRID && ident) {
      const result: boolean = await Mondb.deleteOneData(Table.shopRecord, { SRID });
      if (result) {
        return resSend(res, true, true, "删除成功");
      }
      return resSend(res, false, null, "删除失败");
    }
    return resSend(res, false, null, "删除失败");
  } catch (error) {
    return resSend(res, null, false, "系统错误");
  }
});
export default router;
